package main;


import com.jme3.asset.AssetManager;
import com.jme3.collision.CollisionResult;
import com.jme3.collision.CollisionResults;
import com.jme3.input.RawInputListener;
import com.jme3.input.event.JoyAxisEvent;
import com.jme3.input.event.JoyButtonEvent;
import com.jme3.input.event.KeyInputEvent;
import com.jme3.input.event.MouseButtonEvent;
import com.jme3.input.event.MouseMotionEvent;
import com.jme3.input.event.TouchEvent;
import com.jme3.math.FastMath;
import com.jme3.math.Matrix4f;
import com.jme3.math.Ray;
import com.jme3.math.Vector2f;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.texture.Texture;
import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture;

public class MouseInputListener implements RawInputListener {
 
    private Camera cam;
    private Ray ray;
    private float centralX = 400;
    private float centralY = 280;
    private CollisionResults results;
    private Node scene;
    private boolean mouseClicked;
    private boolean mousePressed;
    private int mouseX, mouseY;
    private AppSettings app;
    private int angle;
    private Geometry clickedObject;
    public boolean clicked = false;
    
    public MouseInputListener(Camera camera, Node selectables, AssetManager assetManager, AppSettings set, Node mouseNode) {
        this.cam = camera;
        this.scene = selectables;
        results = new CollisionResults();
        mouseClicked = false;
        mousePressed = false;
        mouseX = mouseY = 0;
        app = set;
    }
 
    public void onJoyAxisEvent(JoyAxisEvent evt) {
    }
 
    public void onJoyButtonEvent(JoyButtonEvent evt) {
    }
 
    public void onKeyEvent(KeyInputEvent evt) {
    }
 
    public void onMouseButtonEvent(MouseButtonEvent evt) {
        if (evt.isPressed()) {
            mousePressed = true;
            if (mouseClicked) {
                mouseClicked = false;
            }
        } else if (evt.isReleased()) {
            if (mousePressed) {
                mouseClicked = true;
                onMouseClicked();
            }
        }
    }
 
    public void onMouseMotionEvent(MouseMotionEvent evt) {
        mouseX = evt.getX();
        mouseY = evt.getY();
        
        AppSettings settings = app;
            mouseX = (int)FastMath.clamp(mouseX, 0, settings.getWidth());
            mouseY = (int)FastMath.clamp(mouseY, 0, settings.getHeight());       
    }
 
    public boolean analisePick() {
        CollisionResults results = new CollisionResults();
        // Convert screen click to 3d position
        Vector2f click2d = new Vector2f(mouseX, mouseY);
        Vector3f click3d = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 0f).clone();
        Vector3f dir = cam.getWorldCoordinates(new Vector2f(click2d.x, click2d.y), 1f).subtractLocal(click3d);
        // Aim the ray from the clicked spot forwards.
        Ray ray = new Ray(click3d, dir);
        // Collect intersections between ray and all nodes in results list.
        scene.collideWith(ray, results);
        // (Print the results so we see what is going on:)
        //for (int i = 0; i < results.size(); i++) {
          // (For each “hit”, we know distance, impact point, geometry.)
         // float dist = results.getCollision(i).getDistance();
         // Vector3f pt = results.getCollision(i).getContactPoint();
         // String target = results.getCollision(i).getGeometry().getName();
         // System.out.println("Selection #" + i + ": " + target + " at " + pt + ", " + dist + " WU away.");
        //}
        // Use the results -- we rotate the selected geometry.
        if (results.size() > 0) {
          // The closest result is the target that the player picked:
          Geometry target = results.getClosestCollision().getGeometry();
          this.clickedObject = target;
          clicked = true;
          return true;
          //System.out.println("FINAL: "+target.getName());
        }
        return false;
    }
    
    public Geometry getClickedObject() {
        return clickedObject;
    }
    
    public void resetObject() {
        this.clickedObject = null;
    }
    
    private void onMouseClicked() {
        analisePick();
        Main.mapControl.getDude().attack();
        
    }

    public float getAngleFromPointer() {
        return DumbMath.getAngle(mouseX, mouseY, centralX, centralY);
    }
    
    public void beginInput() {
        
    }

    public void endInput() {
        
    }

    public void onTouchEvent(TouchEvent evt) {
        
    }

}